//
//  ChatListView.swift
//  WeChatClient
//
//  Created by AI Assistant on 2025/10/31.
//

import SwiftUI

struct ChatListView: View {
    @EnvironmentObject var chatViewModel: ChatViewModel
    
    var body: some View {
        NavigationView {
            List {
                ForEach(chatViewModel.conversations) { conversation in
                    NavigationLink(destination: ChatDetailView(conversation: conversation).environmentObject(chatViewModel)) {
                        ChatListItemView(conversation: conversation)
                    }
                }
            }
            .navigationTitle("微信")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: {
                        // 添加好友或群聊功能
                    }) {
                        Image(systemName: "plus")
                    }
                }
            }
        }
    }
}

struct ChatListItemView: View {
    let conversation: Conversation
    
    var body: some View {
        HStack(spacing: 12) {
            // 头像
            Image(systemName: conversation.participant.avatar)
                .font(.system(size: 40))
                .foregroundColor(.blue)
                .frame(width: 50, height: 50)
                .background(Color.gray.opacity(0.2))
                .clipShape(RoundedRectangle(cornerRadius: 8))
            
            VStack(alignment: .leading, spacing: 4) {
                HStack {
                    Text(conversation.participant.displayName)
                        .font(.system(size: 16, weight: .medium))
                        .foregroundColor(.primary)
                    
                    Spacer()
                    
                    if let lastMessageTime = conversation.lastMessageTime {
                        Text(formatTime(lastMessageTime))
                            .font(.system(size: 12))
                            .foregroundColor(.secondary)
                    }
                }
                
                HStack {
                    Text(formatLastMessage(conversation.lastMessage))
                        .font(.system(size: 14))
                        .foregroundColor(.secondary)
                        .lineLimit(1)
                    
                    Spacer()
                    
                    if conversation.unreadCount > 0 {
                        Text("\(conversation.unreadCount)")
                            .font(.system(size: 12, weight: .medium))
                            .foregroundColor(.white)
                            .frame(minWidth: 18, minHeight: 18)
                            .background(Color.red)
                            .clipShape(Circle())
                    }
                }
            }
        }
        .padding(.vertical, 4)
    }
    
    private func formatTime(_ date: Date) -> String {
        let now = Date()
        let calendar = Calendar.current
        
        if calendar.isDate(date, inSameDayAs: now) {
            let formatter = DateFormatter()
            formatter.dateFormat = "HH:mm"
            return formatter.string(from: date)
        } else if calendar.isDate(date, inSameDayAs: calendar.date(byAdding: .day, value: -1, to: now) ?? now) {
            return "昨天"
        } else {
            let formatter = DateFormatter()
            formatter.dateFormat = "MM/dd"
            return formatter.string(from: date)
        }
    }
    
    private func formatLastMessage(_ message: ChatMessage?) -> String {
        guard let message = message else { return "" }
        
        switch message.type {
        case .text, .emoji:
            return message.content
        case .image:
            return "[图片]"
        case .system:
            return message.content
        }
    }
}
